<?php
require_once 'phpmoko/lib/common.php';

require_once 'phpmoko/ui/Widget.php';

class Hyperlink extends Widget
{
	private $text;
	private $link;
	private $target = null;
	private $key = -1;
	private $nofollow;

	function Hyperlink($text, $link)
	{
		parent::__construct();
		$this->text = $text;
		$this->link = $link;
		$this->nofollow = false;
	}

	function setNoFollow($mode)
	{
		$this->nofollow = $mode;
	}

	function setTarget($target)
	{
		$this->target = $target;
	}

	function getTarget()
	{
		return $this->target;
	}

	function setText($text)
	{
		$this->text = $text;
	}

	function getText()
	{
		return $this->text;
	}

	function setLink($link)
	{
		$this->link = $link;
	}

	function getLink()
	{
		return $this->link;
	}

	function onKeyPress($key)
	{
		$this->key = $key;
	}

	function toStringA()
	{
		$str = '<a href="' . htmlspecialchars($this->link) . '"';
		if ($this->target != null)
			$str .= ' target="' . $this->target . '"';

		if ($this->nofollow)
			$str .= ' rel="nofollow"';

		if ($this->style != null)
			$str .= ' class="' . $this->style . '"';

		$str .= '>';

		if ($this->text instanceof Widget)
			$str .= $this->text->toString();
		else
			$str .= $this->text;

		$str .= '</a>';

		return $str;
	}

	function toString()
	{
		$str = '';

		$str .= '<div';

		$str .= ' id="' . $this->id . '"';

		$str .= '>';

		$str .= $this->toStringA();

		$str .= '</div>';

		if ($this->key > 0) {
			JQuery::activate();
			RootPanel::get()->addJSCode("$('html').keydown(function(event) { if (event.keyCode != " . $this->key . ") return; this.count = ++this.count || 1; if (this.count == 1) window.location.href=\"" . htmlspecialchars($this->link) . "\";});");
		}

		return $str;
	}
}
?>
